package com.nativetalk.controller.app.four.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.dict.TeacherLanguage;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.common.Constants;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.four.search.FourSearchService;
import com.nativetalk.service.four.teacher.FourTeacherService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class FourSearchController {
    private static Logger logger = Logger.getLogger(FourSearchController.class);
    @Resource
    private FourSearchService fourSearchService;
    @Resource
    private FourTeacherService fourTeacherService;

    /**
     * @return RetInfo
     * @Purpose 查找前10条热门搜索
     * @version 4.0
     * @time 2016-10-27
     * @author lizhun
     */
    @RequestMapping(value = "/app/10/search/hot", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo hot() {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo = fourSearchService.findHotKeyword();

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 学生-首页搜索  搜老师、课程、微课
     * @version 4.0
     * @time 2016-10-31
     * @author lizhun
     */
    @RequestMapping(value = "/app/10/search", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo search(@RequestBody Page page, @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            // 注意：token不是必填参数，如果有token就记录该用户的搜索记录，否则不记录搜索记录。
            //校验token，如果校验通过就从缓存中读取Member
            TdNtMember tdNtMember = null;
            if (StringUtils.isNotBlank(token)) {
                Object o = MemcachedUtils.get(token);
                if (o != null && TdNtMember.class.isInstance(o)) {
                    tdNtMember = (TdNtMember) o;
                }
            }

            RetInfo retInfo = fourSearchService.search(page, tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }


    /**
     * @return RetInfo
     * @Purpose 学生-首页搜索  搜课程
     * @version 4.0
     * @time 2016-10-31
     * @author lizhun
     */
    @RequestMapping(value = "/app/10/search/course", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo course(@RequestBody Page page, @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo = fourSearchService.coursesearch(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 根据课程类别 查找老师 dict_id:1默认排序2教学时间倒叙3,评分倒叙
     * @version 4.0
     * @time 2016-10-31
     * @author lizhun
     */
    @RequestMapping(value = "/app/10/search/teacher", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teacher(@RequestBody Page page) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo = fourTeacherService.findTeacherByCourse(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查找首页老师信息
     * @version 4.1
     * @time 2017-02-09
     * @author wangzheng
     */
    @RequestMapping(value = "/app/12/search/index/teacher/language", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teacherLanguage() {
        try {
            //打印入参
            logger.info(Constants.prefix_in);
            RetInfo retInfo = RetInfo.getSuccessInfo();
            List<TeacherLanguage> languageDtoList = fourTeacherService.findTeacherLanguage();

            retInfo.setObj(languageDtoList);
            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 查找首页老师信息
     * @version 4.1
     * @time 2017-02-09
     *  {
     *      "teacher_language_id":350," // 中教 外教
     *      obj":1,
     *      "currentPage":1,
     *      "dict_id":0
     *  }
     * @author wangzheng
     */
    @RequestMapping(value = "/app/11/search/index/teacher", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo indexTeacher(@RequestBody Page page) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            Integer lanId = page.getTeacher_language_id();
            if (lanId == null || lanId.intValue() == 0) {
                page.setTeacher_language_id(350); //默认查询英语老师
            }

            // 如果cagegoryId=0 查询所分类，那么查询是不添加分类条件，因此将其设为null
            if (page.getCategoryId() != null && page.getCategoryId().intValue() == 0) {
                page.setCategoryId(null);
            }

            RetInfo retInfo = fourTeacherService.findTeacherIndexList(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 课程详细页查找开通该课程的老师信息
     * @version 4.1
     * @time 2017-02-16
     * @author wangzheng
     */
    @RequestMapping(value = "/app/11/search/course/teacher", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo courseTeacher(@RequestBody Page page) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo = fourTeacherService.findTeacherCourseList(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }


    /**
     * @return RetInfo
     * @Purpose 学生-首页搜索  搜微课
     * @version 4.0
     * @time 2016-10-31
     * @author Peter
     */
    @RequestMapping(value = "/app/12/index/search/weike", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo searchWeike(@RequestBody Page page) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));


            RetInfo retInfo = fourSearchService.searchWeike(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }


    /**
     * @return RetInfo
     * @Purpose 学生-首页搜索  搜老师、课程、微课
     * @version 4.3
     * @time 2017-06-19
     * @author Peter
     */
    @RequestMapping(value = "/app/13/search", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo search13(@RequestBody Page page, @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            // 注意：token不是必填参数，如果有token就记录该用户的搜索记录，否则不记录搜索记录。
            //校验token，如果校验通过就从缓存中读取Member
            TdNtMember tdNtMember = null;
            if (StringUtils.isNotBlank(token)) {
                Object o = MemcachedUtils.get(token);
                if (o != null && TdNtMember.class.isInstance(o)) {
                    tdNtMember = (TdNtMember) o;
                }
            }

            RetInfo retInfo = fourSearchService.search13(page, tdNtMember);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 学生-首页搜索  搜课程
     * @version 4.3
     * @time 2017-06-19
     * @author Peter
     */
    @RequestMapping(value = "/app/13/search/course", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo course13(@RequestBody Page page, @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo = fourSearchService.courseSearch13(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }


}


